<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="父菜单" prop="parentId">
                        <el-cascader placeholder="顶级菜单" v-model="value"
                            :props="{value:'menuId',label:'menuName',checkStrictly: true}" :options="options"
                            @change="handleChange" style="width: 100%;">
                        </el-cascader>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="菜单名称" prop="menuName">
                        <el-input v-model="ruleForm.menuName" @blur="getPinyin"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="菜单编号" prop="menuCode">
                        <el-input v-model="ruleForm.menuCode"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="菜单类型" prop="menuType">
                        <el-radio-group v-model="ruleForm.menuType">
                            <el-radio :label="item.id" v-for="(item, index) in menuType" :key="index">
                                {{item.typeName}}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="ruleForm.menuType == 2">
                <el-col :span="12">
                    <el-form-item label="组件路径" prop="componentPath">
                        <el-input v-model="ruleForm.componentPath"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="路由地址" prop="routePath">
                        <el-input v-model="ruleForm.routePath"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
    import { Create } from '../../api/menu';
    import pinyin from 'js-pinyin';
    export default {
        name: 'RbacUiAdminAdd',
        props: {
            menuData: {
                type: Array,
                default: function () {
                    return [];
                }
            },
            menuType: {
                type: Array,
                default: function () {
                    return [];
                }
            },
            rowData: {
                type: Object
            }
        },
        data() {
            return {
                value: [],
                options: [],
                ruleForm: {
                    menuName: "",
                    menuCode: "",
                    menuType: 2,
                    componentPath: "",
                    routePath: "",
                    parentId: 0,
                    parentPath: ""
                },
                rules: {
                    menuName: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ],
                    menuCode: [
                        { required: true, message: '请输入菜单编号', trigger: 'blur' }
                    ],
                    menuType: [
                        { required: true, message: '请选择菜单类型', trigger: 'blur' }
                    ]
                }
            };
        },

        mounted() {
            var reg = new RegExp('\\,"children":\\[]', 'g')
            this.options = JSON.parse(JSON.stringify(this.menuData).replace(reg, ''))
            if (this.rowData.parentPath.length > 0) {
                this.value = `${this.rowData.parentPath},${this.rowData.menuId}`.split(',').map(m => parseInt(m));
            } else {
                this.value = parseInt(`${this.rowData.menuId}`)
            }
        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.value.length > 0) {
                            this.ruleForm.parentId = this.value.slice(-1)[0];
                            this.ruleForm.parentPath = this.value.toString();
                        }

                        Create(this.ruleForm).then(m => {
                            let res = m.data;
                            if (res.code == 0) {
                                this.$message({
                                    message: res.msg,
                                    type: 'warning'
                                });
                            } else {
                                let _this = this;
                                this.$message({
                                    message: '添加成功',
                                    type: 'success',
                                    onClose: function (msg) {
                                        _this.$emit('OnSave', res, 'add');
                                    }
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleChange(value) {
            },
            getPinyin() {
                this.ruleForm.menuCode = pinyin.getFullChars(this.ruleForm.menuName).toLowerCase();
            }
        },
    };
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>